package com.vepick.metier.requetes;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.vepick.beans.Tache;

public class RequetesSurVehicule {
	public static int NombreDeRoutinesVehicule(Connection conn, int immat)
			throws SQLException {
		String requete = "SELECT count(idRoutines) AS nb FROM Routines WHERE IMMATVEHICULE ="
				+ immat;
		int nb = 0;
		Statement stmt = conn.createStatement();
		ResultSet res = stmt.executeQuery(requete);
		while (res.next()) {
			nb = res.getInt("nb");
		}
		res.close();
		return nb;
	}

	public static Tache TacheCourrante(Connection conn, int immat)
			throws SQLException {
		// R�cup�ration de la routine du jour
		int routine = Requetes_routines.consulterRoutineDuJour(conn, immat);
		Tache tache = null;

		// R�cup�ration de la tache courrante
		try {
			String requete = "SELECT * FROM TACHE WHERE idRoutines="
					+ routine
					+ " AND tacheAnnule=0 AND dateFinTache IS NULL ORDER BY seqTache ASC";
			Statement stmt = conn.createStatement();
			ResultSet res = stmt.executeQuery(requete);
			if (res.next()) {
				tache = new Tache(res.getInt("idTache"),
						res.getInt("idStation"), res.getInt("idRoutines"),
						res.getDouble("seqTache"), res.getInt("idTypeTache"),
						null, res.getInt("tacheAnnule"),
						res.getInt("nombrevelo"),
						res.getString("commentairetache"));
			}
			res.close();
		} catch (SQLException e) {
		}
		return tache;
	}

	public static int afficherIDTachesDeLaRoutine(Connection conn, int idRoutine)
			throws SQLException {
		int compteur = 0;

		// Recuperation des differentes taches
		try {
			String requete = "SELECT idTache AS id FROM TACHE WHERE idRoutines="
					+ idRoutine
					+ " AND tacheAnnule=0 AND dateFinTache IS NULL ORDER BY seqTache ASC";
			Statement stmt = conn.createStatement();
			ResultSet res = stmt.executeQuery(requete);
			while (res.next()) {
				System.out.println("-----------------------------");
				System.out.println("Tache numero : " + res.getInt("id"));
				compteur++;
			}
			System.out.println("-----------------------------");
			res.close();
		} catch (SQLException e) {
		}
		return compteur;
	}

	public static int AfficherVeloRegules(Connection conn, int idRoutines)
			throws SQLException {
		String requete = "SELECT E.idVelo AS id FROM estRegulePar E, Tache T WHERE E.idTache = T.idTache AND T.idRoutines="
				+ idRoutines;
		Statement stmt = conn.createStatement();
		ResultSet res = stmt.executeQuery(requete);
		int compteur = 0;
		while (res.next()) {
			System.out.println("---------------------------");
			System.out.println("Vélo numéro : " + res.getInt("id"));
			compteur++;
		}
		System.out.println("---------------------------");
		res.close();
		return compteur;
	}
}
